﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
using System.Windows.Forms;

using com.globalwave.a8ext;
using System.Data.SqlClient;
using System.Data;
using System.Data.OleDb;
using Common;

namespace SalePrinting
{
    public partial class FeePayoutForm : Form
    {
        private FeePayoutService feePayoutService;

        public FeePayoutForm()
        {
            InitializeComponent();
            feePayoutService = FeePayoutService.getInstance();

            //NameValueCollection where = new NameValueCollection();

            //DataTable feePayoutTable = feePayoutService.query("select * from [fee_payout] ", -1, where).Tables[0];

            //setDetails(feePayoutTable.Rows);
        }
        /*
        private void dg_feePayout_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            for(int rowCount = e.RowIndex; rowCount < e.RowIndex + e.RowCount; rowCount++)
           {
               DataGridViewRow row = dg_feePayout.Rows[rowCount];

               NameValueCollection entity = new NameValueCollection();
               entity.Add("type_", getCellValueString(row, 1));
               entity.Add("name_", getCellValueString(row, 2));
               entity.Add("amount_", getCellValueString(row, 3));

               //this.feePayoutService.create(entity);
           }

        }


        private String getCellValueString(DataGridViewRow row, int cellIndex)
        {
            object value = row.Cells[cellIndex].Value;
            if (value == null)
            {
                return "";
            }
            else
            {
                return value.ToString();
            }
        }

        private void setDetails(DataRowCollection details)
        {
            this.dg_feePayout.Rows.Clear();

            foreach (DataRow detail in details)
            {

                int n = dg_feePayout.Rows.Add();

                dg_feePayout.Rows[n].Cells[0].Value = detail["id_"].ToString();
                dg_feePayout.Rows[n].Cells[1].Value = detail["type_"].ToString();

                dg_feePayout.Rows[n].Cells[2].Value = detail["name_"].ToString();
                dg_feePayout.Rows[n].Cells[3].Value = detail["amount_"].ToString();

            }

        }
        */

        private void initDate() {

            dg_feePayout.AutoGenerateColumns = false;
            string connectString = BaseService.getConnectString();

            // OleDbConnection connection = new OleDbConnection(connectString);
            //给dataGridVies赋值
            da = new OleDbDataAdapter("select * from fee_payout", connectString);
            ds = new DataSet();
            da.Fill(ds, "fee_payout");
            this.dg_feePayout.DataSource = ds.Tables["fee_payout"];
        }

        private void FeePayoutForm_Load(object sender, EventArgs e)
        {

            initDate();

            //this.dataGridView1.Columns["id_"].Visible = false;

            /*
            List<ComItem> comItems = new List<ComItem>();
            comItems.Add(new ComItem("", "-- 请输入 --"));
            comItems.Add(new ComItem("F", "收入"));
            comItems.Add(new ComItem("P", "支出"));
            */

            DataTable comItems = new DataTable("tableSource");
            comItems.Columns.AddRange(new DataColumn[] {
                    new DataColumn("Value"),
                    new DataColumn("Text") });
            comItems.Rows.Add("F", "收入");
            comItems.Rows.Add("P", "支出");


            DataGridViewTextBoxColumn textColumn = new DataGridViewTextBoxColumn();
            textColumn.DataPropertyName = "id_";
            textColumn.Name = "id_";
            textColumn.ReadOnly = true;
            textColumn.HeaderText = "标识";
            textColumn.Visible = false;
            this.dg_feePayout.Columns.Add(textColumn);

            DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
            combo.DataSource = comItems;
            combo.DataPropertyName = "type_";
            combo.DisplayMember = "Text";
            combo.ValueMember = "Value";
            combo.HeaderText = "类型";

            //this.dataGridView1.Rows[2].Cells[2] = combo;
            this.dg_feePayout.Columns.Add(combo);

            textColumn = new DataGridViewTextBoxColumn();
            textColumn.DataPropertyName = "name_";
            textColumn.HeaderText = "内容";
            textColumn.MaxInputLength = 100;
            textColumn.Width = 250;
            this.dg_feePayout.Columns.Add(textColumn);

            textColumn = new DataGridViewTextBoxColumn();
            textColumn.DataPropertyName = "amount_";
            textColumn.Name = "amount_";
            textColumn.HeaderText = "金额";
            this.dg_feePayout.Columns.Add(textColumn);

            textColumn = new DataGridViewTextBoxColumn();
            textColumn.DataPropertyName = "process_date";
            textColumn.Name = "process_date";
            textColumn.HeaderText = "日期";
            this.dg_feePayout.Columns.Add(textColumn);
            //textColumn.DefaultCellStyle.NullValue = DateTime.Now.ToShortDateString();

            textColumn = new DataGridViewTextBoxColumn();
            textColumn.DataPropertyName = "process_time";
            textColumn.Name = "process_time";
            textColumn.HeaderText = "时间";
            textColumn.Visible = false;
            this.dg_feePayout.Columns.Add(textColumn);
            //textColumn.DefaultCellStyle.NullValue = DateTime.Now.ToShortTimeString();
            
        }



        OleDbDataAdapter da = null;
        DataSet ds = null
            ;

        private void FeePayoutForm_Leave(object sender, EventArgs e)
        {
            save();
        }

        private void save() {

            //保存(包括增删改。这是批处理。)
            OleDbCommandBuilder sb = new OleDbCommandBuilder(da);
            da.Update(ds.Tables["fee_payout"]);

            initDate();
        }

        private void bt_save_Click(object sender, EventArgs e)
        {
            save();
        }

        private void dg_feePayout_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
        {
            e.Row.Cells["process_date"].Value = DateTime.Now.ToShortDateString();
            e.Row.Cells["process_time"].Value = DateTime.Now.ToLocalTime();
        }

        private void dg_feePayout_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            // Validate the CompanyName entry by disallowing empty strings.
            if (this.dg_feePayout.Columns[e.ColumnIndex].Name == "amount_")
            {
                try
                {
                    Double.Parse(e.FormattedValue.ToString());
                    this.dg_feePayout.Rows[e.RowIndex].ErrorText = null;
                }
                catch
                {
                    if (this.dg_feePayout[e.ColumnIndex, e.RowIndex].IsInEditMode)
                    {
                        this.dg_feePayout.Rows[e.RowIndex].ErrorText = "金额请输入数字";
                        e.Cancel = true;
                    }
                }
            }
        }
    }

}
